package com.pb.news.dao;

import com.pb.news.util.ConfigManager;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.*;

/**
 * BaseDao--BDQN_WEB_NEWS
 *
 * @author: Jovi
 * @createTime: 2018-03-22 15:06
 **/
public class BaseDao {

    protected Connection conn;
    protected PreparedStatement pstmt;
    protected Statement stmt;
    protected ResultSet rs;

    public Connection getConnection2() {

        Context context;
        try {
            context = new InitialContext();
            DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/news");
            conn = ds.getConnection();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }

    public boolean getConnection() {
        try {
            String driver = ConfigManager.getInstance().getString("jdbc.driver_class");
            String url = ConfigManager.getInstance().getString("jdbc.connection.url");
            String username = ConfigManager.getInstance().getString("jdbc.username");
            String password = ConfigManager.getInstance().getString("jdbc.password");

            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }

    public int executeUpdate(String sql, Object[] param) {
        int updateRows = 0;
        getConnection();
        try {
            pstmt = conn.prepareStatement(sql);

            for (int i = 0; i < param.length; i++) {
                pstmt.setObject(i + 1, param[i]);
            }
            updateRows = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return updateRows;
    }

    public ResultSet executeSql(String sql, Object[] params) {
        getConnection();
        try {
            pstmt = conn.prepareStatement(sql);

            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public boolean closeResource() {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return false;
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return false;
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return false;
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return false;

        }
        return true;

    }
}
